package tangyaxin.www.saoerweima;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import com.sam.widget.headerBar.BaseHeaderView;
import com.sam.widget.headerBar.LeftImgTitleRightImgBar;
import java.util.List;
import adapter.ContactInfoAdapter;
import entity.ContactEntity;

/**
 * Created by Administrator on 2016/7/5.
 */
public class EncodeContactActivity extends Activity{
    private LeftImgTitleRightImgBar header;
    private CheckBox contactName,contactAddress;
    private ListView phoneNumLV,emailLV;
    private View genarateQRBtn;

    public static final String KEY_CONTACT_ENTITY = "contact_entity";
    private static final String KEY_PHONE_NUMBER = "phone";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_encode_contact);
        findView();
        initView();
    }

    private void findView() {
        header = (LeftImgTitleRightImgBar) findViewById(R.id.header);
        contactName = (CheckBox) findViewById(R.id.contact_name);
        contactAddress = (CheckBox) findViewById(R.id.contact_address);
        phoneNumLV = (ListView) findViewById(R.id.phone_num_list);
        emailLV = (ListView) findViewById(R.id.email_list);
        genarateQRBtn = findViewById(R.id.generate_qr);
    }

    private void initView() {
        Intent intent = getIntent();
        if(intent != null){
            ContactEntity contactEntity = intent.getParcelableExtra(KEY_CONTACT_ENTITY);
            if(!TextUtils.isEmpty(contactEntity.getName())) contactName.setText(contactEntity.getName());
            List<String> phones = contactEntity.getPhoneNums();
            if(phones != null && phones.size() > 0){
                phoneNumLV.setAdapter(new ContactInfoAdapter(this,phones));
            }
            List<String> emails = contactEntity.getEmails();
            if(emails != null && emails.size() > 0){
                emailLV.setAdapter(new ContactInfoAdapter(this,emails));
            }
            if(!TextUtils.isEmpty(contactEntity.getAddress())) contactAddress.setText(contactEntity.getAddress());
        }

        header.setOnHeaderClickListner(onHeaderClickListener);
        genarateQRBtn.setOnClickListener(onClickListener);
    }


    private BaseHeaderView.OnHeaderClickListener onHeaderClickListener = new BaseHeaderView.OnHeaderClickListener() {
        @Override
        public void onHeaderLeftClicked() {
            finish();
        }

        @Override
        public void onHeaderCenterClicked() {

        }

        @Override
        public void onHeaderRightClicked() {
            startActivity(new Intent(EncodeContactActivity.this,QRMakeActivity.class));
        }
    };

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.generate_qr){
                    startActivity(new Intent(EncodeContactActivity.this,QRMakeActivity.class));
            }
        }
    };

}
